// pages/goos/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
        id:"28ee4e3e60233eec042a252c286436e0",
        img:'/images/goods/menu1.png',
        surplus:10,
        address:'田园食堂一层A2窗口',
        price:4.5,
        title: '五香茶叶蛋',
        remain: 20,
        discribe:'描述信息',
        canteen_id:"",
        canteen_name:"",
        window_name:"",
        window_id:""
  },

  goToSuccess:function() {
    wx.navigateTo({
      url: '/pages/order/succeed/succeed',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // console.log(options);
    // console.log(options.id);
    var meal_id = options.id;
    this.setData({
      id:options.id
    })
    // console.log(that.data.id)
    const db = wx.cloud.database();
    
       // 注释为卢泽华进行的测试
        // db.collection('meal').doc(options.id)
        db.collection('meal').doc(meal_id)
        .get({
          success: function(res) {
            // res.data 包含该记录的数据
            // console.log(res.data);
            var detail  = res.data;
            wx.getStorage({

              key: 'myData',
             
              success: function(res) {
             
                // console.log(res.data.student_id)
                var stuId = res.data.student_id;
                db.collection('student').where({
                  student_id: stuId
                  
                })
                .get({
                  success: function(res) {
                    // res.data 是包含以上定义的两条记录的数组
                    // console.log(res.data[0].card_img_url)
                    if(res.data[0].student_status==1){
                      that.setData({
                        title:detail.meal_name,
                        img:detail.meal_img,
                        surplus:detail.meal_num,
                        price:detail.meal_price,
                        discribe:detail.meal_detail,
                        canteen_id:detail.canteen_id,
                        price:1,
                      })
                    }else{
                      that.setData({
                        title:detail.meal_name,
                        img:detail.meal_img,
                        surplus:detail.meal_num,
                        price:detail.meal_price,
                        discribe:detail.meal_detail,
                        canteen_id:detail.canteen_id,
                        price:detail.meal_price
                      })
                    }
                    // console.log(that.data.canteen_id);
                //查询食堂
                db.collection('canteen').where({
                  canteen_id:that.data.canteen_id
                })
                .get({
                  success: function(res) {
                    // res.data 是包含以上定义的两条记录的数组
                    that.setData({
                      canteen_name:res.data[0].canteen_name
                    })
                    // console.log(that.data.canteen_name);
                    db.collection('window').where({
                      canteen_id:that.data.canteen_id
                    })
                    .get({
                      success: function(res) {
                        // res.data 是包含以上定义的两条记录的数组
                        // console.log(res.data[0].window_name);
                        that.setData({
                          window_name:res.data[0].window_name,
                          window_id:res.data[0].window_id
                        })
    
                        that.setData({
                          address:that.data.canteen_name+that.data.window_name
                        })
                      }
                    });
                  }
                });
                  }
                  
                }) 
                
              },
              fail(){
                that.setData({
                  title:detail.meal_name,
                  img:detail.meal_img,
                  surplus:detail.meal_num,
                  price:detail.meal_price,
                  discribe:detail.meal_detail,
                  canteen_id:detail.canteen_id,
                  price:detail.meal_price
                })
                db.collection('canteen').where({
                  canteen_id:that.data.canteen_id
                })
                .get({
                  success: function(res) {
                    // res.data 是包含以上定义的两条记录的数组
                    that.setData({
                      canteen_name:res.data[0].canteen_name
                    })
                    // console.log(that.data.canteen_name);
                    db.collection('window').where({
                      canteen_id:that.data.canteen_id
                    })
                    .get({
                      success: function(res) {
                        // res.data 是包含以上定义的两条记录的数组
                        // console.log(res.data[0].window_name);
                        that.setData({
                          window_name:res.data[0].window_name,
                          window_id:res.data[0].window_id
                        })
    
                        that.setData({
                          address:that.data.canteen_name+that.data.window_name
                        })
                      }
                    });
                  }
                });
              }
            })
            
          
          }                                                                                                    
        })
    //   }
     
    //  })
     
     
    
    
   

  },

  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 预定按钮
  goToSuccess(){
    var that = this;
    console.log(that.data.id);
    wx.getStorage({
      key: 'myData',
      success (res) {
        console.log(res.data.student_id);
        var student_id = res.data.student_id;
        const db = wx.cloud.database()
        console.log(that.data.id);
        db.collection('order_info').where({
          student_id:student_id,
          order_status:"0"
        })
        .get({
          success: function(res) {
            // res.data 是包含以上定义的两条记录的数组
            console.log(res.data.length)
            if(res.data.length==1){
              wx.showToast({
                title: '您还有未取的餐哦，请先进行取餐',
                icon: 'none',
                duration: 2000
              })
            }else{
              db.collection('meal').doc(that.data.id).get({
                success: function(res) {
                  // res.data 包含该记录的数据
                  console.log(res.data.meal_num);
                  var num = String(res.data.meal_num-1);
                  console.log(typeof num);
                  console.log(num);
                  console.log(that.data.id);
                  wx.cloud.callFunction({
                    // 要调用的云函数名称
                    name: 'update_meal',
                    // 传递给云函数的参数
                    data: {
                      id:that.data.id,
                     num:num
                    },
                    success: res => {
                      console.log(res)
                    }
                  })
                  
                }
              })
               // console.log(res.data.student_id);
              wx.navigateTo({
                url: '/pages/order/succeed/succeed?id='+ that.data.id+'&price='+that.data.price,
              })
        
            }
          }
        
          
              
        })
        
        
       
        
        
        },fail(){
          wx.showToast({
            title: '请先填写信息',
            icon: 'none',
            duration: 2000
          }),
          setTimeout(function () {
          
            wx.navigateTo({
              url: '/pages/prefer/user/user',
              
            })
        
          },2000)
        
        }
      })
  }
})